# -*- encoding=utf8 -*-

import time

from airtest.core.api import *
from apicore.PocoApi import PocoApi
from base_page.phone_home.Phone_Home import Phone_Home

""" 秒装秒玩 """


class OnePieceInstallation(PocoApi):
    def __init__(self):
        super().__init__()
        self.phone_home = Phone_Home()

    # 进入秒装秒玩
    def enter_page(self):
        self.phone_home.one_piece_installation()

    # def everone_piece_installation(self, apptext):
    #     self.enter_page()
    #     for i in apptext:
    #         self.phone_home.poco_click(text=i)
    #         time.sleep(2)

    def everone_piece_installation(self, pagenum, packagelist):
        self.enter_page()
        listsapkname = []
        for i in range(pagenum):
            if i == 0:
                print(type(packagelist[i]))
                listsapkname = packagelist[i]
                for i in packagelist[i]:
                    self.phone_home.poco_click(text=i)
                self.phone_home.poco_click(text="一键安装")
                sleep(5)
                try:
                    # 等待图像出现
                    quxiao = wait(
                        Template(self.base_path + "/images/cloudphonehome/" + "quxiao.png", record_pos=(0.148, 0.146),
                                 resolution=(1080, 2340)), timeout=5, interval=0.5)
                    # 点击找到的图像
                    touch(quxiao)
                except Exception as e:
                    print(f"进入云机失败，错误信息：{e}")
                    log(f"进入云机失败，错误信息：{e}")
                self.issucessppinstall(listsapkname)
                self.exit_cloudin()
            else:
                print("开始第二次")
                listsapkname = []
                listsapkname = packagelist[i]
                self.phone_home.poco_click(text="下一页")
                for i in packagelist[i]:
                    self.phone_home.poco_click(text=i)
                self.phone_home.poco_click(text="一键安装")
                sleep(5)
                try:
                    # 等待图像出现
                    quxiao = wait(
                        Template(self.base_path + "/images/cloudphonehome/" + "quxiao.png", record_pos=(0.148, 0.146),
                                 resolution=(1080, 2340)), timeout=5, interval=0.5)
                    # 点击找到的图像
                    touch(quxiao)
                except Exception as e:
                    print(f"进入云机失败，错误信息：{e}")
                    log(f"进入云机失败，错误信息：{e}")
                self.issucessppinstall(listsapkname)
                self.exit_cloudin()

    # 退出云机
    def exit_cloudin(self):  ##退出云机
        self.phone_home.poco_click("com.unicom.cloudphone:id/iv_more")
        self.phone_home.poco_click(text="退出云手机")

    # 向左复位+
    def resetcloudphonein(self):
        for i in range(1, 5):
            swipe((100, 500), (800, 500))
            # self.phone_home.poco_swipe(start=(100, 500), end=(800, 500))

    ##向右边滑动五页寻找指定图片
    def findcloudinpicture(self, picturepath):
        flag = 0
        for i in range(1, 5):
            if not exists(Template(picturepath)):
                swipe((1000, 400), (400, 400))
            else:
                print(f'******已经找到{picturepath}')
                flag = 1
                break
        self.resetcloudphonein()
        return flag

    # 公共方法判断是否安装成功
    def issucessppinstall(self, appnames):  ##公共方法判断是否安装成功
        picture_rootpath = self.base_path + "/images/cloudphonehome/"
        for appname in appnames:
            picturepath = os.path.join(picture_rootpath, f'{appname}.png')
            state = self.findcloudinpicture(picturepath)
            try:
                if state == 0:
                    print(f'{appname}安装失败')
                if state == 1:
                    print(f'{appname}安装成功')
            except Exception as e:
                print(f"安装失败，错误信息：{e}")
            # logger.info(f'{cloudphonename}的{appname}安装失败')
